package al4th.chp1;

import al4th.StdLib.StdOut;
import al4th.StdLib.StdRandom;

/**
 * @Author : wanghongfu
 * @Date : Created in 下午9:32  17/10/17
 * @Email 1583505981@qq.com
 * @Description:
 */
public class Ex_1_1_15 {
    public static  int[] histogram(int[] a, int M){
        int[] h=new int [M];
        int N=a.length;
        for (int i = 0; i < N; i++) {
            if (a[i]< M){
                h[a[i]]++;
            }
        }
        return h;
    }

    public static void main(String[] args) {
        int N=30;
        int M=10;
        int[] a=new int[N];
        for (int i = 0; i < N; i++) {
            a[i]= StdRandom.uniform(M);
        }
        for (int i = 0; i < N; i++) {
            StdOut.printf("%2d",a[i]);
        }
        System.out.println("\n");
        int[] h=histogram(a,M);
        for (int i = 0; i < M; i++) {
            StdOut.printf("%4d",h[i]);
        }
    }
}
